package com.whz.gateway.common.util;

import lombok.experimental.UtilityClass;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author whz
 */
@UtilityClass
public class RegexUtils {

    /**
     * 邮箱
     */
    private static final Pattern EMAIL = Pattern.compile(
            "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
    /**
     * 身份证
     */
    private static final Pattern ID_CARD = Pattern.compile("^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$");
    /**
     * 以字母开头，长度在6~18之间，只能包含字母、数字和下划线
     */
    private static final Pattern PASSWORD = Pattern.compile("^[a-zA-Z]\\w{5,17}$");
    /**
     * 强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 8-30 之间)：
     */
    private static final Pattern PASSWORD_STRONG_NOT_SPECIAL =
            Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{7,29}$");
    /**
     * 强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-30之间)：
     */
    private static final Pattern PASSWORD_STRONG_SPECIAL = Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{7,29}$");
    /**
     * 账号是否合法(字母开头，允许10-30ge字符，允许字母数字下划线)
     */
    private static final Pattern USER_NAME = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]{9,29}$");
    /**
     * 国内手机号
     */
    public static final String PHONE_REG = "^[1][3-578][0-9]{9}$";
    /**
     * HTML
     */
    public static final String HTML_REGEX = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";

    public static final Pattern HTML_PATTERN = Pattern.compile(HTML_REGEX);

    public static boolean isEmail(String email) {
        return EMAIL.matcher(email).matches();
    }

    public static boolean isIdCard(String id) {
        return ID_CARD.matcher(id).matches();
    }

    public static boolean isSimplePassWord(String pass) {
        return PASSWORD.matcher(pass).matches();
    }

    public static boolean isStrongPassNotSpecial(String pass) {
        return PASSWORD_STRONG_NOT_SPECIAL.matcher(pass).matches();
    }

    public static boolean isStrongPassSpecial(String pass) {
        return PASSWORD_STRONG_SPECIAL.matcher(pass).matches();
    }

    public static boolean isLegalUserName(String username) {
        return USER_NAME.matcher(username).matches();
    }

    public static boolean isHtml(String content) {
        Matcher matcher = HTML_PATTERN.matcher(content);
        return matcher.find();
    }

    /**
     * 获取 html文本中的body内的文本,
     *
     * @param html 输入文本
     * @return body内的文本
     */
    public static String getBodyTextFromHtml(final String html) {
        Element body = Jsoup.parse(html).body();
        String s = body.ownText();
        if (StringUtils.isEmpty(s)) {
            s = body.html();
        }
        Matcher matcher = HTML_PATTERN.matcher(s);
        if (matcher.find()) {
            return StringUtils.trimWhitespace(matcher.replaceAll(""));
        }
        return s;
    }
}